<template>
  <v-dialog :title="title" :close-on-click-modal="false" v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="method==='view'" label-width="140px">
      <el-row>
        <!-- <el-col :span="12">
            <el-form-item label="项目id" prop="project.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择项目id"
            labelName = 'projectName'
            labelValue = 'id'
            v-model = "inputForm.project.id"
            :limit="1"
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :searchs="[
            {
              prop: 'projectName',
              label: '项目名称'
            }
            ]"
            :api="projectMainService">
          </GridSelect>
           </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item label="期号" prop="periodNo" :rules="[
                 ]">
            <el-input v-model="inputForm.periodNo" placeholder="请填写期号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="本期计量金额(含税)" prop="currentMeasureAmt" :rules="[{validator: validator.isNumber}
                  
                 ]">
            <el-input v-model="inputForm.currentMeasureAmt" placeholder="请填写本期计量金额"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="计量单" prop="attachment" :rules="[
                 ]">
            <file-upload v-model="inputForm.attachment"  tip="单个文件不要超过200M"></file-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false" icon="circle-close">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
      </span>
    </template>
  </v-dialog>
</template>

<script>
import GridSelect from '@/components/gridSelect'
import projectMeasureService from '@/api/measure/projectMeasureService'
import projectMainService from '@/api/project/projectMainService'
export default {
  data() {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      projectMainService: projectMainService,
      inputForm: {
        id: '',
        project: {
          id: ''
        },
        periodNo: '',
        currentMeasureAmt: '',
        attachment: '',
        fileName: '',
        
      }
    }
  },
  components: {
    GridSelect
  },
  watch: {
    'inputForm.attachment': {
      handler(newVal) {
        if (newVal) {
          // 获取所有文件信息
          const files = newVal.split(',');
          const fileNames = [];
          const fileUrls = [];

          files.forEach(file => {
            if (file&&file.indexOf(',')) {
              const urlArr = file.split('&fileName=');
              if (urlArr.length >= 2) {
                const fileName = urlArr[urlArr.length - 1];
                const fileUrl = urlArr[0];
                fileNames.push(fileName);
                fileUrls.push(fileUrl);
              } else {
                return;
              }
            }
          });         
          if (fileNames.length) {
            this.inputForm.fileName = fileNames.join(',');
            // 保存纯文件地址（不带文件名）
            // this.inputForm.attachment = fileUrls.join(',');
          }
        }
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    init(method, id) {
      this.method = method

      if (method === 'add') {
        this.inputForm.id = '';
        this.inputForm.project.id = id
        this.title = `新建项目计量`
      } else if (method === 'edit') {
        this.inputForm.id = id
        this.title = '修改项目计量'
      } else if (method === 'view') {
        this.inputForm.id = id
        this.title = '查看项目计量'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          projectMeasureService.queryById(this.inputForm.id).then((data) => {
            this.inputForm = this.recover(this.inputForm, data)
            this.loading = false
          })
        }
      })
    },
    handleUploadSuccess(res, file) {
      this.inputForm.fileName = file.name;
    },
    // 表单提交
    doSubmit() {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          projectMeasureService.save(this.inputForm).then((data) => {
            this.visible = false
            this.$message.success(data)
            this.$emit('refreshDataList')
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

  
